package cloud.dandanit.itservice.service.impl;

import cloud.dandanit.itservice.entity.Article;
import cloud.dandanit.itservice.entity.Image;
import cloud.dandanit.itservice.mapper.ArticleMapper;
import cloud.dandanit.itservice.service.ArticleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程简介 服务实现类
 * </p>
 *
 * @author luokai
 * @since 2022-07-05
 */
@SuppressWarnings("UnnecessaryLocalVariable")
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    /**
     * 获取前台首页文章方法
     * @return 图片列表
     */
    @Override
    public List<Article> getIndexArticle() {

        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("state", 1)
                    .orderByDesc("view_num")
                    .last("limit 10")
                    .select("id","title","summary","sort","difficulty","state","view_num","gmt_create","gmt_modified");

        List<Article> articleList = this.list(queryWrapper);

        return articleList;
    }

    @Override
    @Cacheable(key="#articleId", value = "article")
    public Article getArticleById(String articleId) {

        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",articleId);

        Article article = this.getOne(queryWrapper);

        return article;
    }
}
